package com.itheima.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.PageResult;
import com.itheima.dto.Simple;
import com.itheima.dto.SubjectsDirectorysDto;
import com.itheima.mapper.SubjectsDirectorysMapper;
import com.itheima.mapper.SubjectsMapper;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.Subjects;
import com.itheima.pojo.SubjectsDirectorys;
import com.itheima.pojo.User;
import com.itheima.service.SubjectsDirectorysService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SubjectsDirectorysServiceImpl implements SubjectsDirectorysService {
    @Autowired
    private SubjectsDirectorysMapper subjectsDirectorysMapper;

    @Autowired
    private SubjectsMapper subjectsMapper;

    @Autowired
    private UserMapper userMapper;
    @Override
    public void modifySubjectsDirectorysById(SubjectsDirectorys subjectsDirectorys) {
        subjectsDirectorysMapper.updateById(subjectsDirectorys);
    }


    @Transactional
    @Override
    public Integer addSubjectsDirectorys(SubjectsDirectorys subjectsDirectorys) {

        subjectsDirectorysMapper.insert(subjectsDirectorys);
        //先查出学科id,修改学科里面的目录数
        Subjects subjects = subjectsMapper.selectById(subjectsDirectorys.getSubjectID());
        subjects.setTwoLevelDirectory(subjects.getTwoLevelDirectory()+1);
        subjectsMapper.updateById(subjects);
        return subjectsDirectorys.getId();
    }

    @Override
    public PageResult subjectsDirectorysPage(Integer page, Integer pagesize, Integer subjectID, String directoryName, Integer state) {

        IPage<SubjectsDirectorys> p = new Page<>(page,pagesize);

        LambdaQueryWrapper<SubjectsDirectorys> wapper = new LambdaQueryWrapper<>();
        wapper.eq(null!=subjectID, SubjectsDirectorys::getSubjectID,subjectID)
                .like(null!=directoryName && ""!=directoryName, SubjectsDirectorys::getDirectoryName,directoryName)
                .eq(null!=state, SubjectsDirectorys::getState,state);
        IPage<SubjectsDirectorys> subjectsDirectorysIPage = subjectsDirectorysMapper.selectPage(p, wapper);
        List<SubjectsDirectorys> records = subjectsDirectorysIPage.getRecords();
        String s = JSON.toJSONString(records);
        List<SubjectsDirectorysDto> subjectsDirectorysDtos = JSON.parseArray(s, SubjectsDirectorysDto.class);
        subjectsDirectorysDtos.stream().forEach(c->{
            User user = userMapper.selectById(c.getCreatorID());
            Subjects subjects = subjectsMapper.selectById(c.getSubjectID());
            c.setUsername(user.getUsername());
            c.setSubjectName(subjects.getSubjectName());
        });
        return new PageResult((int)subjectsDirectorysIPage.getTotal(),pagesize, (int) subjectsDirectorysIPage.getPages(),page,subjectsDirectorysDtos);
    }

    @Transactional
    @Override
    public void removeSubjectsDirectorysById(Integer id) {
        //先查出学科id,修改学科里面的目录数
        SubjectsDirectorys subjectsDirectorys = subjectsDirectorysMapper.selectById(id);
        Subjects subjects = subjectsMapper.selectById(subjectsDirectorys.getSubjectID());
        subjects.setTwoLevelDirectory(subjects.getTwoLevelDirectory()-1);
        subjectsMapper.updateById(subjects);
        subjectsDirectorysMapper.deleteById(id);
    }

    @Override
    public void modifySubjectsDirectorysState(Integer id, Integer state) {
        SubjectsDirectorys subjectsDirectorys = new SubjectsDirectorys();
        subjectsDirectorys.setId(id);
        subjectsDirectorys.setState(state);
        subjectsDirectorysMapper.updateById(subjectsDirectorys);
    }

    @Override
    public SubjectsDirectorysDto findSubjectsDirectorysById(Integer id) {
        SubjectsDirectorysDto subjectsDirectorysDto = (SubjectsDirectorysDto) subjectsDirectorysMapper.selectById(id);
        User user = userMapper.selectById(subjectsDirectorysDto.getCreatorID());
        subjectsDirectorysDto.setUsername(user.getUsername());
        Subjects subjects = subjectsMapper.selectById(subjectsDirectorysDto.getSubjectID());
        subjectsDirectorysDto.setSubjectName(subjects.getSubjectName());
        return subjectsDirectorysDto;
    }

    @Override
    public List<Simple> subjectsDirectorysSimple(Integer subjectID) {
        List<Simple> simples =  subjectsDirectorysMapper.selectIdAndNameByid(subjectID);
        return simples;
    }
}
